package asf.core
{
	import asf.controllers.FrontController;
	import asf.interfaces.ITransition;
	
	internal class History
	{
		private var items:Array;
		private var nav:Navigation;
		private var _currentIndex:uint = 0;
		
		public function History( p_nav:Navigation )
		{
			items = new Array( );
			nav = p_nav;
		}
		
		public function dispose( ):void
		{
			items = null;
			nav = null;
			_currentIndex = 0;
		}
		
		public function push( section:*, transition:ITransition, transitionOut:ITransition, initObject:Object ):void
		{
			items.push( new HistoryItem( section, transition, transitionOut, initObject, new Date( ) ) );
			_currentIndex = items.length - 1;
			
			FrontController.broadcastEvent( AppGlobalCommands.HISTORY_CHANGED );
		}
		
		public function cut( ):void
		{
			items.splice( currentIndex + 1);
		}
		
		public function next( ):Boolean
		{
			return go( 1 );
		}
		
		public function back( ):Boolean
		{
			return go( - 1 );
		}
		
		public function canGoFoward( ):Boolean
		{
			return currentIndex < items.length - 1;
		}
		
		public function canGoBack( ):Boolean
		{
			return currentIndex > 0;
		}
		
		public function clear( ):void
		{
			items = new Array( );
		}
		
		public function get currentIndex( ):uint
		{
			return _currentIndex;
		}
		
		public function goToIndex( index:int ):Boolean
		{
			var item:HistoryItem = items[ index ];
			
			if( item != null )
			{
				_currentIndex = index;
				nav.changeTo( item.section, item.transition, item.transitionOut, item.initObject, false );
				
				FrontController.broadcastEvent( AppGlobalCommands.HISTORY_CHANGED );
			}
			
			
			return item != null;
		}
		
		public function go( amount:int ):Boolean
		{
			return goToIndex( currentIndex + amount );
		}
		
		public function translate( ):Array
		{
			var result:Array = new Array( );
			var item:HistoryItem;
			
			for each( item in items )
			{
				result.push( { section: item.section, transition: item.transition, transitionOut: item.transitionOut, time: item.time } );
			}
			
			return result;
		}
	}
}

import asf.interfaces.ITransition;

internal class HistoryItem
{
	public var section:*;
	public var transition:ITransition;
	public var transitionOut:ITransition;
	public var time:Date;
	public var initObject:Object;
	
	public function HistoryItem( p_section:*, p_transition:*, p_transitionOut:*, p_initObject:Object, p_time:Date ):void
	{
		section = p_section;
		transition = p_transition;
		transitionOut = p_transitionOut;
		time = p_time;
		initObject = p_initObject;
	}
}